﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhiStore.Threading;
using FileSystemAnalysis.Code;

namespace FileSystemAnalysis
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private Model model = null;
        List<object> channels = null;


        private void runButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string basepath = folderBrowserDialog.SelectedPath;
                List<Boostable> threads = new List<Boostable>();

                model = new Model();
                Channel<string> fromProducerToSizeWorkers = new Channel<string>(200000);
                Channel<ResultCapsule> fromSizeWorkersTointermediateConsumer = new Channel<ResultCapsule>(50000);
                Channel<string> fromIntermediateConsumerToShaWorkers = new Channel<string>(200000);
                Channel<ResultCapsule> fromShaWorkersToConsumer = new Channel<ResultCapsule>(50000);

                threads.Add(new Producer(basepath, fromProducerToSizeWorkers, model));
                threads.Add(new FileSizeWorker(fromProducerToSizeWorkers, fromSizeWorkersTointermediateConsumer));
                threads.Add(new IntermediateConsumer(model, fromSizeWorkersTointermediateConsumer, fromIntermediateConsumerToShaWorkers));
                threads.Add(new Sha256Worker(fromIntermediateConsumerToShaWorkers, fromShaWorkersToConsumer));
                threads.Add(new Consumer(fromShaWorkersToConsumer, model));

                channels = new List<object>();
                channels.Add(fromProducerToSizeWorkers);
                channels.Add(fromSizeWorkersTointermediateConsumer);
                channels.Add(fromIntermediateConsumerToShaWorkers);
                channels.Add(fromShaWorkersToConsumer);

                Booster.Boost(threads);
                timer.Start();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (model != null)
            {
                if (model.Running && model.Indexing)
                {
                    statusLabel.Text = string.Format("Running {0} indexed", model.FilesIndexed);
                }
                else if (model.Running && model.Shaing)
                {
                    statusLabel.Text = string.Format("Running {0}/{1} SHA256", model.FilesShaed, model.SentForSha);
                }
                else if (model.Running)
                {
                    statusLabel.Text = string.Format("Running {0}", model.Count);
                }
                else
                {
                    timer.Stop();
                    dataGridView.DataSource = model.Data;
                    channels = null;
                }
            }
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            if (channels != null)
                foreach (object obj in channels)
                    if (obj is Channel<string>)
                        (obj as Channel<string>).Poison();
                    else
                        (obj as Channel<ResultCapsule>).Poison();
        }
    }
}
